﻿using Domain.InvoiceTicket.Ticket;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.InvoiceTicket.Invoice;
using MES.Server.API.Dto.InvoiceTicketDTO;
using ReturnValue;

namespace MES.Server.API.Application.Handler.InvoiceTicket.Invoice
{
    public class GetTicketHandler : IRequestHandler<GetTicketCommand, ApiPaging<GetTicketDto>>
    {
        private readonly IRepository<TicketModel> _ticketRepo;

        public GetTicketHandler(IRepository<TicketModel> ticketRepo)
        {
            _ticketRepo = ticketRepo;
        }

        public Task<ApiPaging<GetTicketDto>> Handle(GetTicketCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiPaging<GetTicketDto>();
            var query= _ticketRepo.GetAll().Where(x=>!x.IsDel);
            var linq = from a in query
                       select new GetTicketDto()
                       {
                           Id = a.Id,
                           TicketCode=a.TicketCode,
                           TicketName=a.TicketName,
                           TicketType=a.TicketType,
                           TicketDate=a.TicketDate,
                           TicketMomeny = a.TicketMomeny,
                           ReceiveTicket = a.ReceiveTicket,
                           TicketDesc = a.TicketDesc
                       };
            res.Totalcount= linq.Count();
            res.TotalPage= linq.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            res.Message = "查询成功";
            res.Code = StatusCode.Success;
            return Task.FromResult(res);
        }
    }
}
